package cn.tedu.ivos2.user.service.Impl;

import cn.tedu.ivos2.base.exception.ServiceException;
import cn.tedu.ivos2.base.response.StatusCode;
import cn.tedu.ivos2.user.mapper.UserMapper;
import cn.tedu.ivos2.user.pojo.dto.UserLoginDTO;
import cn.tedu.ivos2.user.pojo.dto.UserSaveDTO;
import cn.tedu.ivos2.user.pojo.dto.UserSelectDTO;
import cn.tedu.ivos2.user.pojo.entity.User;
import cn.tedu.ivos2.user.pojo.vo.UserLoginVO;
import cn.tedu.ivos2.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public UserLoginVO login(UserLoginDTO userLoginDTO) {
        User user=new User();
        user.setUsername(userLoginDTO.getUsername());
        if (userMapper.select(user)==null){
            throw new ServiceException(StatusCode.USERNAME_ALREADY_EXISTS);
        }
        user.setPassword(userLoginDTO.getPassword());
        User userAll= userMapper.select(user);
       if (userAll==null){
           throw new ServiceException(StatusCode.PASSWORD_ERROR);
       }
       UserLoginVO userLoginVO=new UserLoginVO();
       BeanUtils.copyProperties(userAll,userLoginVO);
       return userLoginVO;
    }

    @Override
    public void insert(UserSaveDTO userSaveDTO) {
        User user=new User();
        BeanUtils.copyProperties(userSaveDTO,user);
        user.setUpdateTime(LocalDateTime.now());
        if (user.getId()==null) {
            user.setPassword("123");
            user.setCreateTime(LocalDateTime.now());
            userMapper.insert(user);
        }else {
            userMapper.update(user);
        }
    }

    @Override
    public List<UserLoginVO> query(UserSelectDTO userSelectDTO) {
        return userMapper.queryBySelectDTO(userSelectDTO);
    }

    @Override
    public void resetPwd(Long id) {
        User user=new User();
        user.setId(id);
        user.setPassword("admin");
        user.setUpdateTime(LocalDateTime.now());
        log.debug("用户_数据链路层:账号和密码:{}",user);
        userMapper.update(user);
    }

    @Override
    public void update(Long id, String status) {
        User user=new User();
        user.setId(id);
        user.setStatus(status);
        user.setUpdateTime(LocalDateTime.now());
        log.debug("用户_数据链路层:id和状态:{}",user);
        userMapper.update(user);
    }

    @Override
    public void deleteById(Long id) {
        userMapper.deleteByUserId(id);
    }

    @Override
    public List<UserLoginVO> selctAudit(Long parentId) {
        ArrayList<UserLoginVO> userVOList=new ArrayList<>();
        //当前用户的上级
        UserLoginVO auditUser1=userMapper.selectById(parentId);
        userVOList.add(auditUser1);
        //上级的上级

        if (auditUser1!=null&& auditUser1.getParentId()!=null){
            log.debug("审批人的上级id:{}",auditUser1.getParentId());
            UserLoginVO auditUser2=userMapper.selectById(auditUser1.getParentId());
            userVOList.add(auditUser2);
        }
        return userVOList;
    }
}
